import requests
import sqlite3
import os

paperFolder = "./papers/"
filenames = os.listdir(paperFolder)

conn = sqlite3.connect('abstractUrl.db')
cursor = conn.cursor()
cursor.execute("SELECT title, url FROM urls")
for row in cursor.fetchall():
    title, link = row
    filename = title + '.pdf'
    filename = filename.replace('"', '_')
    filename = filename.replace(':', '_')
    filename = filename.replace('\n', '')
    filename = filename.replace('\\', '')
    filename = filename.replace('/', '_')
    if filename in filenames:
        continue
    link = link.replace('..', '')
    try:
        response = requests.get(link, stream=True)
        response.raise_for_status()
        with open(paperFolder + filename, 'wb') as f:
            for chunk in response.iter_content(chunk_size=8192):
                f.write(chunk)
        print(f"Downloaded {filename}")
    except Exception as e:
        print(f"Error downloading {title}: {e}")
conn.close()
